﻿using System.Collections.Generic;
using com.loto.Common.DBUtility;
using com.huigo.Framework.Model;
using System.Data.SqlClient;
using System.Data;
using System;

namespace com.huigo.Framework.BLL
{
    public class UserInfo
    {
        public static Model.UserInfo GetUserInfoByLoginNameAndPwd(string loginName,string pwd)
        {
            Model.UserInfo userInfo = new Model.UserInfo();
            List<string> fields = new List<string>()
            {
                "USER_ID",
                "USER_NAME",
                "USER_REAL_NAME",
                "USER_ROLE_FIDS"
            };
            userInfo.UserName = loginName;
            userInfo.UserPwd = pwd;
            userInfo.UserIsActive = (int)Model.Dict.IsActive.Yes;
            return SqlServerDal.UserInfo.GetItem(userInfo, fields, null, null);
        }

        public static List<Model.UserInfo> GetUserDropList()
        {
            List<string> fields = new List<string>()
            {
                "USER_ID",
                "USER_REAL_NAME"
            };
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                new WhereStatement("USER_IS_ACTIVE","=",((int)Dict.IsActive.Yes).ToString(),"and"),
                new WhereStatement("USER_ROLE_FIDS","like","'%,"+((int)Dict.UserRole.Sales).ToString()+",%'","")
            };
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("USER_REAL_NAME",true)
            };
            return SqlServerDal.UserInfo.GetItems(fields, whereList, orderList, null);
        }
        public static List<Model.UserInfo> GetUserInfo(string userName, int pageSize, int pageIndex)
        {
            List<string> fields = new List<string>()
            {
                "USER_ID",
                "USER_REAL_NAME",
                "USER_DEPT_FID",
                "USER_NAME",
                "USER_PWD",
                "USER_ROLE_FIDS",
                "USER_NUM",
                "USER_CREATED_BY",
                "USER_CREATED_ON",
                "USER_LAST_ACTIVED_ON ",
                "USER_IS_ACTIVE",
                "DEPT_NAME"
            };
            List<WhereStatement> whereList = new List<WhereStatement>();
            if (!string.IsNullOrEmpty(userName))
            {
                whereList.Add(new WhereStatement("USER_REAL_NAME", "like", "'%" + userName + "%' or USER_NAME='%" + userName + "%'", "AND"));
            }
            whereList.Add(new WhereStatement("USER_IS_ACTIVE", "=", ((int)Dict.IsActive.Yes).ToString(), "AND"));
            whereList.Add(new WhereStatement("DEPT_IS_ACTIVE", "=", ((int)Dict.IsActive.Yes).ToString(), ""));
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("USER_REAL_NAME",true)
            };
            List<JoinStatement> join = new List<JoinStatement>();
            join.Add(new JoinStatement("LEFT", "HG_USER_DEPT", "DEPT_ID=USER_DEPT_FID"));
            List<Model.UserInfo> userInfo = null;
            int recordCount = 0;
            int pageCount = 0;
            userInfo = SqlServerDal.UserInfo.GetItemsByProc(fields,join, whereList, orderList, "[PROC_PAGINATION_OUT]", pageSize, pageIndex, out recordCount, out pageCount, null);
            userInfo.ForEach(delegate(Model.UserInfo info)
            {
                info.RecordCount = recordCount;
                info.PageCount = pageCount;
            });
            return userInfo;
        }
        public static bool AddUserInfo(Model.UserInfo InsInfo)
        {
            return SqlServerDal.UserInfo.Insert(InsInfo, null);
        }
        public static bool GetUserInfoList(long userID, string userName,string userRealName)
        {
            bool isExist = false;
            List<string> fields = new List<string>()
            {
                "USER_ID"
            };
            List<SqlParameter> paraList = new List<SqlParameter>();
            SqlParameter para = new SqlParameter("@USER_NAME", SqlDbType.NVarChar, 20);
            para.Value = userName;
            paraList.Add(para);

             para = new SqlParameter("@USER_REAL_NAME", SqlDbType.NVarChar, 10);
             para.Value = userRealName;
            paraList.Add(para);
            List<WhereStatement> whereList = new List<WhereStatement>();
            whereList.Add(new WhereStatement("USER_IS_ACTIVE", "=", "1", " and "));
            whereList.Add(new WhereStatement("USER_REAL_NAME", "=", "@USER_REAL_NAME or USER_NAME=@USER_NAME", ""));

            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("USER_REAL_NAME",true)
            };
            List<Model.UserInfo> insList = SqlServerDal.UserInfo.GetItems(fields, null, whereList, orderList, paraList.ToArray(), null);
            if (insList != null && insList.Count > 0)
            {
                if (userID == long.MinValue)
                    isExist = true;
                else
                {
                    foreach (Model.UserInfo c in insList)
                    {
                        if (userID != c.UserId)
                            isExist = true;
                    }
                }
            }
            return isExist;
        }
        public static bool UpdateUserInfo(long sid, Model.UserInfo usInfo)
        {
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                    new WhereStatement("USER_ID","=",sid.ToString(),"")
            };
            return SqlServerDal.UserInfo.Update(usInfo, whereList, null);
        }
        public static Model.UserInfo GetUserInfoById(long id)
        {
            Model.UserInfo userInfo = new Model.UserInfo();
            userInfo.UserId = id;
            userInfo.UserIsActive = ((int)Dict.IsActive.Yes);
            userInfo.DeptISActive = ((int)Dict.IsActive.Yes);
            List<string> fields = new List<string>() 
             {
              "USER_ID",
                "USER_REAL_NAME",
                "USER_DEPT_FID",
                "USER_NAME",
                "USER_PWD",
                "USER_ROLE_FIDS",
                "USER_NUM",
                "USER_CREATED_BY",
                "USER_CREATED_ON",
                "USER_LAST_ACTIVED_ON ",
                "USER_IS_ACTIVE",
                "DEPT_NAME"
            };
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("USER_REAL_NAME",true)
            };
            List<JoinStatement> join = new List<JoinStatement>();
            join.Add(new JoinStatement("LEFT", "HG_USER_DEPT", "DEPT_ID=USER_DEPT_FID"));
            return SqlServerDal.UserInfo.GetItem(userInfo, fields, join, null);
        }

        //更新user_info里的最后登录时间
        public static bool updateUserInfoLastTime(long userID)
        {
            Model.UserInfo user =new  Model.UserInfo();
            user.UserLastActivedOn = DateTime.Now;
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                new WhereStatement("USER_ID","=",userID.ToString(),"")
            };
            return SqlServerDal.UserInfo.Update(user, whereList, null);
        }
    }
}
